import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
// import addMenu from '../components/common/addMenu'
import axios from '../components/common/http'
// import Cookies from 'js-cookie'

const _import = require('./_import_' + process.env.NODE_ENV)
Vue.use(Router)

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      components: {
        content: _import('login/login')
      },
      meta: {
        requireAuth: true
      }
    },
    {
      path: '*',
      components: {content: _import('404')},
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/addGroup',
      name: 'addGroup',
      components: {content: _import('addGroup/addGroup')},
      meta: {
        requireAuth: true
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (!store.state.user.token && to.path !== '/login') {
    next({
      path: '/login',
      query: {redirect: to.fullPath}
    })
  }
  // console.info(store.state.user.menuData)
  if (!store.state.user.menu.menuData && store.state.user.token) {
    axios.post('/index/pullUser')
      .then(res => {
        next({...to, replace: true})
      })
      .catch(err => {
        console.info(err)
      })
  } else {
    if (!to.meta.requireAuth) {
      next({path: '/'})
      console.info('这里不可以进去')
    }
    next()
  }
})
export default router
